#include "prdfform.h"
#include "ui_prdfform.h"
#include "CourseOutline.h"
#include <QFileDialog>
#include <QFileInfo>
#include <QPrinter>
#include <QTextDocument>

PrdfForm::PrdfForm(QWidget *parent, AdminData *newData) :
    QWidget(parent),
    ui(new Ui::PrdfForm)
{
    ui->setupUi(this);
    this->data=newData;


       this->cList=(data->getCourses());
       //show previously created holidays

       int j=0;
       while(j<cList.size())
       {
           ui->listWidget->addItem(cList[j].getCode());
           j++;
       }
}

PrdfForm::~PrdfForm()
{
    delete ui;
}


//Eric's stuff goes here
void PrdfForm::on_pushButton_clicked()
{

    //place in list selected
    int listInt=ui->listWidget->currentRow();
    if(listInt<0)
        listInt=0;

    //cList is name of vector where courses are stored\

    Course *course = &(cList[listInt]);
    CourseOutline *outline = &(course->getOutline());
    QVector<OutlineSection> sections = outline->getOutlineSectionList();
    QString output("<center><font size=\"6\">");
    output.append(outline->getTitle());
    output.append("</font></center><br><br><br>");
    for (int i = 0; i < sections.size(); i++) {
        output.append("<font size=\"5\"><b>");
        output.append(sections[i].getTitle());
        output.append("</b></font><br><br><font size=\"4\">");
        output.append(sections[i].getText());
        output.append("<br><br></font>");
    }
    output.append("</font>");
    QTextDocument *printDocument = new QTextDocument();
    printDocument->setHtml(output);

    QString fileName = QFileDialog::getSaveFileName(0, tr("Export PDF"), QString(course->getName()), tr("*.pdf"));
    if (!fileName.isEmpty()) {
        if (QFileInfo(fileName).suffix().isEmpty())
            fileName.append(".pdf");
        QPrinter printer(QPrinter::HighResolution);
        printer.setOutputFormat(QPrinter::PdfFormat);
        printer.setOutputFileName(fileName);
        printDocument->print(&printer);
    }

}

//go to course form
void PrdfForm::on_pushButton_2_clicked()
{
    AdminCourseForm *aCF=new AdminCourseForm(this, data);
    aCF->show();
    aCF->topLevelWidget();
}

//go to calendar
void PrdfForm::on_pushButton_3_clicked()
{
    AdminMain *aM=new AdminMain(this, data);
    aM->show();
    aM->topLevelWidget();
}

//go to outline section
void PrdfForm::on_pushButton_4_clicked()
{
    AdminOutSectForm *aOSF=new AdminOutSectForm(this, data);
    aOSF->show();
    aOSF->topLevelWidget();
}
